/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import java.io.*;
import java.io.FileInputStream;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;

import org.xml.sax.SAXException;
import java.io.IOException;

public class SLD2OWLTranslator {

    /** 
     * Main class: Create the parser to convert the xml file in a Dom Structure, 
     * translate the elements in the file, and store it in a xml file
     *
     */
    SLDXML2OWLConversion handler;

    public SLD2OWLTranslator() {
    }

    public void translate(String[] args) throws IOException {

        try {

            String nsName =
                    "http://www.cc.uah.es/ie/ont/sld-ospace#";

            String inputName =
                    args[0];
            String outputName =
                    args[1];



            InputStream ontIS =
                    new FileInputStream(inputName);
            OutputStream ontOS =
                    new FileOutputStream(outputName);
            if (ontIS == null) {
                System.out.println("Ontology dont found");
            }

            OntModel ont =
                    ModelFactory.createOntologyModel();

            ont.read(ontIS, "");

            DOMParser parser =
                    new DOMParser();

            for (int i =
                    2; i < args.length; i++) {
                System.out.println("Traduciendo el fichero: " + (i - 2));
                String inputNameXML =
                        args[i];
                try {
                    parser.parse(inputNameXML);
                } catch (SAXException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                Document document =
                        parser.getDocument();

                translateDocument(document, ont, nsName, ontOS);


            }
            handler.storeOntology(ont, ontOS);

        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    /**
     * Call to conversor, which create the ontology from the xml file
     * @param cnode The node of the Dom structure         
     * @param ont The ontology
     * @param nsName The namespace
     * @param outputFile The ouput ontology file
     */
    public void translateDocument(Node cnode, OntModel ont, String nsName,
            OutputStream outputFile) {
        handler =
                new SLDXML2OWLConversion(cnode, ont, nsName, outputFile);

    }
}
